import random
from typing import List

import numpy as np

from agent import Agent
from simulation import Simulation
from wall import Wall


def init_agents(n: int):
    agents_hashmap = {}
    des = np.array([5, -1])

    for name in range(1, n + 1):
        pos = np.array([0.3 + 8 * random.random(), 0.3 + 8 * random.random()])
        while True:
            flg = True
            for _, other in agents_hashmap.items():
                if np.linalg.norm(pos - other.position) < agent.radius + other.radius:
                    flg = False
                    break
            if flg:
                break
            pos[0], pos[1] = 10 * random.random(), 10 * random.random()

        agent = Agent(name).set_position(pos).set_destination(des)
        agents_hashmap[name] = agent

    return agents_hashmap


def init_walls():
    point_1 = [0, 0]
    point_2 = [0, 10]
    point_3 = [10, 10]
    point_4 = [10, 0]
    point_5 = [5.5, 0]
    point_6 = [4.5, 0]
    wall_1 = Wall(point_1, point_2)
    wall_2 = Wall(point_2, point_3)
    wall_3 = Wall(point_3, point_4)
    wall_4 = Wall(point_4, point_5)
    wall_5 = Wall(point_6, point_1)

    walls: List[Wall] = [wall_1, wall_2, wall_3, wall_4, wall_5]

    return walls


def init():
    agent_hashmap = init_agents(100)
    walls = init_walls()
    return Simulation(agent_hashmap, walls)
